package isssr.entity.src;

import java.io.Serializable;
import java.lang.reflect.Field;

public class User  implements Serializable{

	private Integer uid;
	private String 	username;
	private String 	password;
	private String 	email;
	private String 	firstname;
	private String 	lastname;
	private Role 	role;
	
	// this constructor is for Hibernate
	private User() {}
	
	public User(String username, Role role)
	{
		this.username = new String(username);
		this.role = role;
	}
	
	public User(UserDTO data)
	{
		this.username 	= new String(data.getUsername());
		this.email 		= new String(data.getEmail());
		this.firstname 	= new String(data.getFirstname());
		this.lastname	= new String(data.getLastname());
		this.role 		= data.getUserRole();
	}


	private Integer getUid() {
		return uid;
	}

	private void setUid(Integer id) {
		this.uid = id;
	}

	private String getUsername() {
		return username;
	}

	private void setUsername(String username) {
		this.username = username;
	}

	private String getPassword() {
		return password;
	}

	private void setPassword(String password) {
		this.password = password;
	}

	private String getEmail() {
		return email;
	}

	private void setEmail(String email) {
		this.email = email;
	}

	private String getFirstname() {
		return firstname;
	}

	private void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	private String getLastname() {
		return lastname;
	}

	private void setLastname(String lastname) {
		this.lastname = lastname;
	}

	private Role getRole() {
		return role;
	}

	private void setRole(Role role) {
		this.role = role;
	}
	
	protected boolean isValidPassword(String password)
	{
		if(password == null || password.length() == 0)
			return false;
		
		return true;
	}
	
	public boolean setFirstPassword(String password)
	{
		if(this.password != null || !isValidPassword(password))
			return false;
		
		this.password = new String(password);
		return true;
	}
	
	public boolean changePassword(String oldPassword, String newPassword)
	{
		if(!isValidPassword(oldPassword) || !isValidPassword(newPassword))
		{
			return false;
		}
		
		if(! oldPassword.equals(this.password))
		{
			return false;
		}
		
		this.password = new String(newPassword);
		return true;
	}
	
	public Integer compareLevel(User user)
	{
		return this.role.compare(user.role);
	}
	
	public UserDTO getDTO()
	{
		UserDTO data = new UserDTO(this.username, this.email, this.firstname, this.lastname, this.role);
		return data;
	}
	
	@Override
	public boolean equals(Object o)
	{
		if(o==null)
			return false;
		if(! (o instanceof User))
			return false;
		
		User otherUser = (User) o;
		
		Field[] userFields = this.getClass().getDeclaredFields();
		
		Object value1;
		Object value2;
		
		for(Field field : userFields)
		{
			try
			{
				value1 = field.get(this);
				value2 = field.get(otherUser);
				
				if((value1 == null && value2 != null)
						|| ( value1 != null && !value1.equals(value2) ))
					return false;
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
		}
		
		return true;
	}
	
	@Override
	public String toString()
	{
		String output = "User[";
		output = output.concat( uid != null ? ""+uid : "");
		output = output.concat( ", ");
		output = output.concat( username != null ? username : "");
		output = output.concat( ", ");
		output = output.concat( password != null ? ""+password.hashCode() : "");
		output = output.concat( ", ");
		output = output.concat( email != null ? email : "");
		output = output.concat( ", ");
		output = output.concat( firstname != null ? firstname : "");
		output = output.concat( ", ");
		output = output.concat( lastname != null ? lastname : "");
		output = output.concat( ", ");
		output = output.concat( role != null ? role.toString() : "");
		output = output.concat( "]");
		
		return output;
	}
	
}
